package org.check.UI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import org.check.potimize.business.LoadBaseCourseUser;

import net.miginfocom.swing.MigLayout;

public class ExportXlsPanel extends JPanel {

	private static JTextField text_xlsUri;
	private JButton button_doExportXls;
	private String configName="name";
	private String startDate="start";
	private String endDate="end";
	

	/**
	 * Create the panel.
	 * @param colSelectPanel 
	 */
	public ExportXlsPanel(final ColSelectPanel colSelectPanel) {
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setBackground(new Color(173, 216, 230));
		
		setLayout(new MigLayout("", "[91px][178.00][100px][100px]", "[25px]"));

		button_doExportXls = new JButton("导出");
		button_doExportXls.setToolTipText("仅输入文件名将导出到查数工具目录下");
		add(button_doExportXls, "cell 3 0,growx,aligny center");
		button_doExportXls.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				List<String> colList = colSelectPanel.getCheckedElements();
				String dir = getExcelPath();
				if (LoadBaseCourseUser.isExport)
					LoadBaseCourseUser.geneExcel(dir, colList);
				JOptionPane.showMessageDialog(null, "Excel已导出到" + getExcelPath() , "导出Excel",
						JOptionPane.PLAIN_MESSAGE);
			}
		});

		text_xlsUri = new JTextField();
		add(text_xlsUri, "cell 1 0,growx,aligny center");
		text_xlsUri.setColumns(10);
		text_xlsUri.setText(getConfigName()+"_"+getStartDate()+"_"+getEndDate()+".xls");

		JLabel lblNewLabel = new JLabel("Excel导出路径");
		add(lblNewLabel, "cell 0 0,grow");

		JButton button_viewXlsUri = new JButton("浏览");
		add(button_viewXlsUri, "cell 2 0,growx,aligny center");
		button_viewXlsUri.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser();
				jfc.setBounds(400, 200, 100, 100);
				jfc.setVisible(true);
				jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				jfc.showDialog(new JLabel(), "确定");
				File file = jfc.getSelectedFile();
				if (file.isDirectory()) {
					setExcelPath(file.getAbsolutePath() + "\\" + text_xlsUri.getText());
				} else if (file.isFile()) {
					setExcelPath(file.getAbsolutePath());
				}
			}
		});
	}

	public String getExcelPath() {
		return text_xlsUri.getText();
	}

	public static void setExcelPath(String txt) {
		text_xlsUri.setText(txt);
	}

	public String getConfigName() {
		return configName;
	}

	public void setConfigName(String configName) {
		this.configName = configName;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public void updateText() {
		text_xlsUri.setText(getConfigName()+"_"+getStartDate()+"_"+getEndDate()+".xls");
	}

}
